package com.woniu.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.order.client.CommentQueryClient;
import com.woniu.order.client.result.CommentQueryResultInfo;
import com.woniu.order.service.CommentService;
import com.woniu.order.service.dto.CommentDto;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class CommentQueryApiImpl implements CommentQueryClient {

    @Resource
    private CommentService commentService;

    /**
     * 根据商品ID查询关于此商品的评论
     *
     * @param productId
     * @return
     */
    @Override
    public Result<List<CommentQueryResultInfo>> searchCommentsByProductId(Integer productId) {
        log.info("根据商品ID查询关于此商品的评论：{}", productId);
        List<CommentDto> commentDtos = commentService.searchCommentsByProductId(productId);
        List<CommentQueryResultInfo> commentQueryResultInfos = BeanUtil.copyToList(commentDtos, CommentQueryResultInfo.class);
        return Result.getInstanceList(CommentQueryResultInfo.class).setData(commentQueryResultInfos);
    }

    /**
     * 根据用户ID查询此用户的所有评论
     *
     * @param userId
     * @return
     */
    @Override
    public Result<List<CommentQueryResultInfo>> searchCommentsByUserId(Integer userId) {
        log.info("根据商品ID查询关于此商品的评论：{}", userId);
        List<CommentDto> commentDtos = commentService.searchCommentsByUserId(userId);
        List<CommentQueryResultInfo> commentQueryResultInfos = BeanUtil.copyToList(commentDtos, CommentQueryResultInfo.class);
        return Result.getInstanceList(CommentQueryResultInfo.class).setData(commentQueryResultInfos);
    }

}
